package com.app.mingluxing.babytree;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.app.mingluxing.R;
import com.app.mingluxing.adapter.ClassifyAdapter;
import com.app.mingluxing.adapter.ClassifyRightAdapter;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.VerRes;
import com.app.mingluxing.bean.VerResTwo;
import com.app.mingluxing.bean.VerResTwoList;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import java.util.ArrayList;
import java.util.List;


/**
 * 选择标题
 * Created by Aaron on 15/7/30.
 */
public class ChooseTitleActivity extends BaseActivity {

    private ClassifyRightAdapter rightAdapter;
    private ListView lv_left;
    private ListView lv_right;
    private static final int CHOOSE_TITLE = 0x10003;
    private List<VerResTwo> treeType;
    private List<VerResTwoList> towList;
    //一级分类的adapter
    private ClassifyAdapter leftAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_title);
        treeType = new ArrayList<VerResTwo>();

        towList = new ArrayList<VerResTwoList>();

        initWidget();

        getThemeTitle();

    }

    private void initWidget() {

        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.choose_style));

        tv_left.setOnClickListener(this);

        lv_left = (ListView) findViewById(R.id.lv_left);
        lv_right = (ListView) findViewById(R.id.lv_right);

        rightAdapter = new ClassifyRightAdapter(this, towList);

        lv_right.setAdapter(rightAdapter);

        lv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (treeType == null || treeType.size() < 1)
                    return;

                towList = treeType.get(i).towList;

                for (int j = 0; j < treeType.size(); j++) {

                    if (j == i)
                        treeType.get(j).isSelect = 1;
                    else
                        treeType.get(j).isSelect = 0;
                }

                leftAdapter.notifyDataSetChanged();

                rightAdapter.setTowList(towList);
                rightAdapter.notifyDataSetChanged();


                if (!lv_right.isStackFromBottom()) {
//                    listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
                    lv_right.setStackFromBottom(true);
                }
                lv_right.setStackFromBottom(false);

            }
        });

        lv_right.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (towList == null || towList.size() < 1)
                    return;

                Intent data = new Intent();
                data.putExtra("themeID", towList.get(i).id);
                data.putExtra("tName", towList.get(i).typeName);
                setResult(CHOOSE_TITLE, data);
                finish();
            }
        });

    }

    /**
     * 获得标题
     */
    private void getThemeTitle() {

        VerRes verRes = (VerRes) CommonUtils.readObj(Constant.VERDATA);

        if (verRes == null || verRes.data == null)
            return;


        if (verRes.data.treeType != null && verRes.data.treeType.size() > 0) {

            treeType = verRes.data.treeType;

            leftAdapter = new ClassifyAdapter(ChooseTitleActivity.this, treeType);
            lv_left.setAdapter(leftAdapter);
            //右边的列表 默认显示的是第一个
            towList = treeType.get(0).towList;
            treeType.get(0).isSelect = 1;

            rightAdapter.setTowList(towList);
            rightAdapter.notifyDataSetChanged();
        }


//        VersionBean versionBean = new VersionBean();
//        versionBean.ver = sp.getString(Constant.BUILD, "0");
//        versionBean.token = sp.getString(Constant.TOKEN, "");
//
//        HttpUtil.getInstance().setPostRequest("加载中...", this, Constant.WEB_ROOT + Constant.BUILD, JSONObject.toJSONString(versionBean),
//
//                 new HttpUtil.SuccessListener() {
//                    @Override
//                    public void setOnSuccessListener(String str) {
//                        if (CommonUtils.isNull(str)){
//                            showToast(Constant.NODATA);
//                            return;
//                        }
//
//                        try {
//
//                            VerRes verRes = JSONObject.parseObject(str, VerRes.class);
//
//                            if (verRes.code == Constant.SUCCESES) {
////                                LogUtil.e("TAG", "解析成功===");
//                                if (verRes.data.treeType != null && verRes.data.treeType.size()>0) {
//
//                                    treeType = verRes.data.treeType;
//
//                                    leftAdapter = new ClassifyAdapter(ChooseTitleActivity.this, treeType);
//                                    lv_left.setAdapter(leftAdapter);
//                                    //右边的列表 默认显示的是第一个
//                                    towList = treeType.get(0).towList;
//
//                                    rightAdapter.setTowList(towList);
//
//                                    rightAdapter.notifyDataSetChanged();
//                                }else {
//                                    showToast(Constant.NOMOREDATA);
//                                    return;
//                                }
//
//                                }else {
//                                showToast(Constant.showErrorMsg(verRes.code, ChooseTitleActivity.this));
//                            }
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            showToast(Constant.NODATA);
//                        }
//                    }
//                }, null);

    }

    public void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);       //统计时长
    }

    public void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

    }

}
